--
-- NewDailyRareRender
-- yu.yu
-- 2019.1.11
--

local ClassRef = app.mvc.createRenderClass(...)

local funcIdList = {[1] = Constant.FUNC_TYPE_DUNGEONS, [2] = Constant.FUNC_TYPE_PVP, [3] = Constant.FUNC_TYPE_ACTIVITY_DUNGEONS}
local RARE_TIPS1 = {"restrictedreward4", "restrictedreward5", "restrictedreward6"}
local RARE_TIPS2 = {"restrictedreward4_1_surface", "restrictedreward5_1_surface", "restrictedreward6_1_surface"}
local RARE_TIPS3 = {"restrictedreward4_3_surface1", "restrictedreward5_3_surface1", "restrictedreward6_3_surface1"}
local RARE_TIPS4 = {"restrictedreward4_3_surface2", "restrictedreward5_3_surface2", "restrictedreward6_3_surface2"}
local BoxTypeByIndex = {[1] = Enums.BoxType.dungeon, [2] = Enums.BoxType.pvp, [3] = Enums.BoxType.eventBox}
local LootTypeByIndex = {[1] = Enums.lootType.dungeonLuckBox, [2] = Enums.lootType.arenaLuckBox, [3] = Enums.lootType.eventLuckBox}

function ClassRef.create(node)
    local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.DailyNewRareRender))
	if node == nil then
		ccui.Helper:doLayout(sprite)
	end
	sprite:init()
	return sprite
end

function ClassRef:init(data)
    self.super.init(self, data)

    self._ui.bg_1:setVisible(false)
    self._ui.bg_2:setVisible(false)
    self._ui.bg_3:setVisible(false)
    self._ui["Panel_1/Panel_icon/icon_1"]:setVisible(false)
    self._ui["Panel_1/Panel_icon/icon_2"]:setVisible(false)
    self._ui["Panel_1/Panel_icon/icon_3"]:setVisible(false)

    self._timeline = self:getTimeline()
    self:pauseAction()

    self:refresh()
end

function ClassRef:refresh()
    if self._data then
        local index = self._data
        self._ui["Panel_1/bg_"..index]:setVisible(true)
        self._ui["Panel_1/Panel_icon/icon_"..index]:setVisible(true)
        self._ui["Panel_1/tlt"]:setString(L(RARE_TIPS1[index]))
        self._ui["Panel_1/tip_tlt"]:setString(L(RARE_TIPS2[index]))
        self._ui["Panel_1/tip_1"]:setString(L(RARE_TIPS3[index]))
        self._ui["Panel_1/tip_2"]:setString(L(RARE_TIPS4[index]))

        self._ui["Panel_1/btn_open"]:addClickEventListener(function()
            executeFunc(self._onClick, "gotoFeature", funcIdList[index])
        end)

        self._ui["Panel_1/Panel_icon"]:addClickEventListener(function()
            executeFunc(self._onClick, "gotoFeature", funcIdList[index])
        end)
    end
end

function ClassRef:refreshBoxNum(index)
    if index then
        local maxNum1 = UD:getLuckBoxDailyMaxNum(BoxTypeByIndex[index])
        local num1 = UD:getLuckBoxRestTimesByType(BoxTypeByIndex[index])
        self._ui.Panel_1:findChild("btn_open/tips_1_0"):setString(string.format( L("dailyLuckboxdaily"), num1, maxNum1 ))
        -- 每周次数
        local maxNum2 = UD:getLuckBoxWeeklyMaxNum(BoxTypeByIndex[index])
        local num2 = UD:getLuckBoxWeeklyRestTimesByType(BoxTypeByIndex[index])
        self._ui.Panel_1:findChild("btn_open/tips_2_0"):setString(string.format( L("dailyLuckboxweekly"), num2, maxNum2 ))
        -- 拥有数量
        local haveNum = UD:getDaibiValue(LootTypeByIndex[index])
        if haveNum == 0 then
            self._ui.Panel_1:getChildByName("bg_num"):setVisible(false)
            self:pauseAction()
        else
            self._ui.Panel_1:getChildByName("bg_num"):setVisible(true)
            self._ui.Panel_1:findChild("bg_num/num"):setString(haveNum)
            --self:playAction()
            self:pauseAction()
            if num1 > 0 and num2 > 0 then --还有可开启的次数用红点
                self._ui.Panel_1:findChild("bg_num/bg_1"):setVisible(true)
                self._ui.Panel_1:findChild("bg_num/bg_2"):setVisible(false)
            else -- 没有可开启的次数用黑点
                self._ui.Panel_1:findChild("bg_num/bg_1"):setVisible(false)
                self._ui.Panel_1:findChild("bg_num/bg_2"):setVisible(true)
            end
        end
    end
end

function ClassRef:playAction()
	if self._timeline then
        self._timeline:gotoFrameAndPlay(0, true)
	end
end

function ClassRef:pauseAction()
	if self._timeline then
    	self._timeline:gotoFrameAndPause(0)
	end
end

function ClassRef:onBtnClick(cb)
    self._onClick = cb
end

return ClassRef